

import java.io.DataInputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.net.UnknownHostException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.hogofwar.DJbot.BasePlugin;
import com.hogofwar.DJbot.IRCBase;

public class COM_title implements BasePlugin {

	public void start(final IRCBase irc, final String[] extra, String channel,
			String sender, String hostname, boolean usename) {
		// System.out.println(extra[0]);
		// System.out.println(extra[1]);
		Boolean online = false;
		String start = "";
		if (usename == true) {
			start = sender + ": ";
		}
		// System.out.println("not"+ extra[0]);

		if (extra.length != 0) {
			if (extra[0] != " ") {
				System.out.println("here");

				String add = null;
				if (!extra[0].contains("http://")) {
					add = "http://" + extra[0];
				} else {
					add = extra[0];
				}
				String tosend = null;
				if (exists(add)) {
					// address.getCanonicalHostName()
					String html = null, tmp;
					try {
						URL url = new URL(add);
						HttpURLConnection urlConnection = (HttpURLConnection) url
								.openConnection();
						urlConnection.setFollowRedirects(true);
						DataInputStream dis = new DataInputStream(
								urlConnection.getInputStream());
						html = "";
						tmp = "";
						while ((tmp = dis.readUTF()) != null) {
							html += " " + tmp;
						}
						dis.close();
					} catch (MalformedURLException e) {
						// TODO Auto-generated catch block
						// e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						// e.printStackTrace();
					}

					html = html.replaceAll("\\s+", " ");
					Pattern p = Pattern.compile("<title>(.*?)</title>");
					Matcher m = p.matcher(html);
					try {
						while (m.find() == true) {
							System.out.println(m.group(1));
							tosend = m.group(1).trim();
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						// e.printStackTrace();
					}
				} else {
					tosend = "The website appears to be down, cannot retrieve title.";
				}

			
				irc.sendMessage(channel, start + "" + tosend);
			} else {
			
				irc.sendMessage(channel, start
						+ "You need to specify a website!");
			}
		} else {
		
			irc.sendMessage(channel, start + "You need to specify a website!");
		}

	}

	public String help() {
		return "Title - title [website] - Says the title of the website.";
		// TODO Auto-generated method stub

	}

	public static boolean exists(String URLName) {
		try {
			HttpURLConnection.setFollowRedirects(true);
			// note : you may also need
			// HttpURLConnection.setInstanceFollowRedirects(false)
			HttpURLConnection con = (HttpURLConnection) new URL(URLName)
					.openConnection();
			con.setRequestMethod("HEAD");
			return (con.getResponseCode() == HttpURLConnection.HTTP_OK);
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	public String version() {
		return "Title version 1";
		// TODO Auto-generated method stub

	}

	@Override
	public String restrict() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Boolean hide() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String aliases() {
		// TODO Auto-generated method stub
		return "getthetitle, gettitle, getitle, titleget";
	}

}
